using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

namespace ClientTools.Server.Webservices
{
    /// <summary>
    /// Summary description for ClientToolsMaintenanceService
    /// </summary>
    [WebService(Namespace = "http://www.codeplex.com/ClientTools4Umbraco/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class ClientToolsMaintenanceService : ClientToolsBaseService
    {

        private ClientTools.Library.Logging.LogManager _LogManager = new ClientTools.Library.Logging.LogManager(umbraco.GlobalSettings.DbDSN);
        private ClientTools.Library.Stats.StatsManager _StatsManager = new ClientTools.Library.Stats.StatsManager(umbraco.GlobalSettings.DbDSN);
        private ClientTools.Library.Versions.VersionManager _VersionManager = new ClientTools.Library.Versions.VersionManager(umbraco.GlobalSettings.DbDSN);

        ///// <summary>
        ///// Authenticates the specified user.
        ///// </summary>
        ///// <param name="UserName">Name of the user.</param>
        ///// <param name="Password">The password.</param>
        ///// <returns></returns>
        //private void Authenticate(string userName, string password)
        //{
        //    base.Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
        //}

        #region "Logging"

        /// <summary>
        /// Gets the log infos.
        /// </summary>
        /// <param name="UserName">Name of the user.</param>
        /// <param name="Password">The password.</param>
        /// <returns></returns>
        [WebMethod]
        public Library.Logging.LogInfoCollection GetLogInfos(string userName, string password)
        {
            base.Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return _LogManager.GetLogInfos();
        }

        /// <summary>
        /// Gets all logs.
        /// </summary>
        /// <param name="UserName">Name of the user.</param>
        /// <param name="Password">The password.</param>
        /// <param name="StartDate">The start date.</param>
        /// <param name="EndDate">The end date.</param>
        /// <param name="LogHeader">The log header.</param>
        /// <param name="UserID">The user ID.</param>
        /// <param name="NodeID">The node ID.</param>
        /// <param name="MaxRows">The max rows.</param>
        /// <returns></returns>
        [WebMethod]
        public Library.Logging.LogCollection GetLogs(string userName, string password, System.Nullable<DateTime> startDate, System.Nullable<DateTime> endDate, string logHeader, System.Nullable<int> userId, System.Nullable<int> nodeId, System.Nullable<int> maxRows, string comment)
        {
            base.Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return _LogManager.GetLogs(startDate, endDate, logHeader, userId, nodeId, maxRows, comment);
        }

        /// <summary>
        /// Deletes the logs by id.
        /// </summary>
        /// <param name="UserName">Name of the user.</param>
        /// <param name="Password">The password.</param>
        /// <param name="LogIDs">The log IDs.</param>
        /// <returns></returns>
        [WebMethod]
        public int DeleteLogsById(string userName, string password, int[] logIds)
        {
            base.Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return _LogManager.DeleteLogsByIDs(logIds);
        }

        /// <summary>
        /// Deletes the logs by header.
        /// </summary>
        /// <param name="UserName">Name of the user.</param>
        /// <param name="Password">The password.</param>
        /// <param name="LogHeader">The log header.</param>
        /// <returns></returns>
        [WebMethod]
        public int DeleteLogsByHeader(string userName, string password, string logHeader)
        {
            base.Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return _LogManager.DeleteLogsByHeader(logHeader);
        }

        /// <summary>
        /// Deletes the logs by date.
        /// </summary>
        /// <param name="UserName">Name of the user.</param>
        /// <param name="Password">The password.</param>
        /// <param name="Date">The date.</param>
        /// <returns></returns>
        [WebMethod]
        public int DeleteLogsByDate(string userName, string password, DateTime date)
        {
            base.Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return _LogManager.DeleteLogsByDate(date);
        }

        #endregion

        #region "Versions"

        /// <summary>
        /// Deletes the versions by date.
        /// </summary>
        /// <param name="UserName">Name of the user.</param>
        /// <param name="Password">The password.</param>
        /// <param name="Date">The date.</param>
        /// <returns></returns>
        [WebMethod]
        public Library.Versions.VersionInfo DeleteVersionsByDate(string userName, string password, DateTime date)
        {
            base.Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return _VersionManager.DeleteVersionsByDate(date);
        }

        /// <summary>
        /// Deletes the versions by number of items.
        /// </summary>
        /// <param name="UserName">Name of the user.</param>
        /// <param name="Password">The password.</param>
        /// <param name="NumberOfItems">The number of items.</param>
        /// <returns></returns>
        [WebMethod]
        public Library.Versions.VersionInfo DeleteVersionsByNumberOfItems(string userName, string password, int numberOfItems)
        {
            base.Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return _VersionManager.DeleteVersionsByNumberOfItems(numberOfItems);
        }

        /// <summary>
        /// Gets the version infos.
        /// </summary>
        /// <param name="UserName">Name of the user.</param>
        /// <param name="Password">The password.</param>
        /// <returns></returns>
        [WebMethod]
        public Library.Versions.VersionInfo GetVersionInfos(string userName, string password)
        {
            base.Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return _VersionManager.GetVersionInfos();
        }

        #endregion

        #region "Stats"

        /// <summary>
        /// Deletes the stats by date.
        /// </summary>
        /// <param name="UserName">Name of the user.</param>
        /// <param name="Password">The password.</param>
        /// <param name="Date">The date.</param>
        /// <returns></returns>
        [WebMethod]
        public Library.Stats.StatsInfo DeleteStatsByDate(string userName, string password, DateTime date)
        {
            base.Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return _StatsManager.DeleteStatsByDate(date);
        }

        /// <summary>
        /// Gets the stats info.
        /// </summary>
        /// <param name="UserName">Name of the user.</param>
        /// <param name="Password">The password.</param>
        /// <returns></returns>
        [WebMethod]
        public Library.Stats.StatsInfo GetStatsInfo(string userName, string password)
        {
            base.Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return _StatsManager.GetStatsInfo();
        }

        #endregion
    }
}
